<?php

class Cat_model extends Model {
	
	private $table 	= 'categorys';
	
	function Cat_model() {
		parent::Model ();
	}
	
	function list_cat_combo( $type = 0, $cat = 0 ) {
		$data = array();
		
		$query = $this->db->query("SELECT `id`, `type`, `id_parent`, `lever`, `name`, `description`, `orders`, `home`, `status`, `style`, `link` FROM `$this->table` WHERE `type` = '$type' AND `id_lang` = '".get_admin_lang()."' AND `id_parent` = '$cat' ORDER BY `orders` ASC, `id` DESC");
		
		if ( $query->num_rows () > 0 ) {
			$data = $query->result_array();
		}
		
		$query->free_result ();
		return $data;
	}
	
	function select_cat_lever( $cat_id ) {
		$data	= array();
		$query 	= $this->db->query("SELECT `lever` FROM `$this->table` WHERE `id` = '$cat_id' AND `id_lang` = '".get_admin_lang()."' LIMIT 1 ");	
		
		if ( $query->num_rows () > 0 ) 
			$data = $query->row_array();
		
		$query->free_result ();
				
		return $data;
	}
	
	function write_cat( $type = 0 ) {
		
		$cat_id = isset( $_POST['cat'] ) ? (int)$_POST['cat'] : 0; 
		
		if ( $cat_id == 0 )
			$lever = 0;
		else {			
			$row 	= $this->select_cat_lever( $cat_id );
			$lever	= $row['lever'] + 1;
		}
		
		$data = array(
			'id_lang'		=> get_admin_lang(),		
			'type'			=> $type,		
			'id_parent'		=> $cat_id,	
			'lever'			=> $lever,	
			'name'			=> isset( $_POST['name'] ) ? htmlspecialchars( $_POST['name'] ) : '',		
			'description'	=> isset( $_POST['description'] ) ? htmlspecialchars( $_POST['description'] ) : '',		
			'status'		=> isset( $_POST['status'] ) ? (int)$_POST['status'] : 0,		
			'orders'		=> 0,		
			'date'			=> time(),
			'style'			=> isset( $_POST['css'] ) ? htmlspecialchars( $_POST['css'] ) : '',			
			'link'			=> isset( $_POST['link'] ) ? htmlspecialchars( $_POST['link'] ) : ''			
		);
		
		$this->db->insert( $this->table , $data );
	}
	
	function update_cat_status( $total = 0, $type = 0 ) {
	
		for( $k = 1; $k <= $total ; $k++ ) {
			
			$status = @(int)$_POST['status_'.$k];
			$order 	= @(int)$_POST['order_'.$k];	
			$id		= @(int)$_POST['id_'.$k]; 
			$home	= @(int)$_POST['home_'.$k]; 
			
			$data = array(				
				'orders'	=> $order,
				'home'		=> $home,
				'status'	=> $status
			);
			
			$this->db->where('id', $id );	
			$this->db->where('id_lang', get_admin_lang() );
			$this->db->where('type', $type );
			
			$this->db->update( $this->table, $data);
		}
	}
	
	function del_cat( $id = 0, $type = 0 ) {		
		$this->db->where('id', $id );
		$this->db->where('id_lang', get_admin_lang() );
		$this->db->where('type', $type );
		$this->db->delete( $this->table );		 
	}
	
	function select_cat( $cat_id = 0, $type = 0 ) {
		$data	= array();
		$query 	= $this->db->query("SELECT `id`, `type`, `id_parent`, `lever`, `name`, `description`, `status`, `style`, `link` FROM `$this->table` WHERE `id` = '$cat_id' AND `type` = '$type' AND `id_lang` = '".get_admin_lang()."' LIMIT 1 ");	
		
		if ( $query->num_rows () > 0 ) 
			$data = $query->row_array();
		
		$query->free_result ();
				
		return $data;
	}
	
	function update_cat( $type = 0 ) {
		
		$cat_id = isset( $_POST['cat'] ) ? (int)$_POST['cat'] : 0;
		
		if ( $cat_id == 0 )
			$lever = 0;
		else {			
			$row 	= $this->select_cat_lever( $cat_id );
			$lever	= $row['lever'] + 1;
		}
		
		$data = array(
			'id_parent'		=> $cat_id,	
			'lever'			=> $lever,	
			'name'			=> isset( $_POST['name'] ) ? htmlspecialchars( $_POST['name'] ) : '',		
			'description'	=> isset( $_POST['description'] ) ? htmlspecialchars( $_POST['description'] ) : '',		
			'status'		=> isset( $_POST['status'] ) ? (int)$_POST['status'] : 0,
			'style'			=> isset( $_POST['css'] ) ? htmlspecialchars( $_POST['css'] ) : '',
			'link'			=> isset( $_POST['link'] ) ? htmlspecialchars( $_POST['link'] ) : ''
		);
		
		$id = isset( $_POST['hid'] ) ? $_POST['hid'] : 0;
		
		$this->db->where('id', $id );
		$this->db->where('type', $type );
		$this->db->where('id_lang', get_admin_lang() );
		
		$this->db->update( $this->table , $data );	
	}

}
?>